package ar.utn.frba.ia.IA_TP1.genetica;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.jgap.FitnessFunction;
import org.jgap.Gene;
import org.jgap.IChromosome;
import org.jgap.impl.CompositeGene;

import ar.utn.frba.ia.IA_TP1.App;
import ar.utn.frba.ia.IA_TP1.mapas.AmericaDelNorte;
import ar.utn.frba.ia.IA_TP1.mapas.AmericaDelSur;
import ar.utn.frba.ia.IA_TP1.mapas.Mapa;

public class MapFitness extends FitnessFunction {
	private static final long serialVersionUID = 1L;
	private List<Mapa> mapas;
	
	public MapFitness(){
		mapas = new ArrayList<Mapa>();
	}

	@Override
	protected double evaluate(IChromosome a_subject) {
		double fitness = 65;
		Gene[] paises = a_subject.getGenes();
		Iterator<Mapa> itMapa = mapas.iterator();
		int i = 0;
		while(itMapa.hasNext()){
			Mapa mapa = itMapa.next();
			for (int[] dupla : mapa.getAdyascencias()) {
				if(!tienenMismoColor(dupla[0], dupla[1], ((CompositeGene)paises[i]).getGenes())){
					fitness++;
				}else{
					fitness--;
				}

			}
			i++;
		}
		
		Gene Mexico = (Gene)((CompositeGene)paises[App.MAPAS.AmN.getNumero()]).getGenes().get(AmericaDelNorte.PaisesAmN.MEXICO.toInt());
		Gene Colombia = (Gene)((CompositeGene)paises[App.MAPAS.AmS.getNumero()]).getGenes().get(AmericaDelSur.PaisesAmS.COLOMBIA.toInt());
		
		if(!Mexico.getAllele().equals(Colombia.getAllele())){
			fitness++;
		}else{
			fitness--;
		}
		
		return fitness;
	}

	private boolean tienenMismoColor(int i, int j,List<Gene> paises) {
		return paises.get(i).getAllele().equals(paises.get(j).getAllele());
	}

	public void addMapas(List<Mapa> mapas2) {
		this.mapas.addAll(mapas2);
	}

}
